package com.maruping.disk.file.service.impl;

import com.maruping.disk.file.entity.SearchFileEntity;
import com.maruping.disk.file.repository.FileDao;
import com.maruping.disk.file.service.FileIoService;
import com.maruping.disk.file.service.FileService;
import com.maruping.disk.file.service.io.FileIoServiceImpl;
import com.maruping.disk.mybatis.entity.PtFileEntity;
import com.maruping.disk.mybatis.flexigrid.PagingEntity;
import com.maruping.disk.mybatis.flexigrid.TblResponseEntity;
import com.maruping.disk.mybatis.ztree.ZtreeEntity;
import com.obs.services.ObsClient;
import com.obs.services.model.TemporarySignatureRequest;
import com.obs.services.model.TemporarySignatureResponse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class FileServiceImpl implements FileService {
    @Resource
    private FileDao fileDao;
    @Override
    public void saveFileInfo(List<PtFileEntity> ptFileEntityList) {
        fileDao.deleteFileAll();
        fileDao.saveFileInfo(ptFileEntityList);

    }
    @Resource
    private FileIoService fileIoService; // 注入FileIoService，复用ObsClient

    @Override
    public TblResponseEntity<PtFileEntity> queryFileInfoListByPage(SearchFileEntity entity, PagingEntity paging) {
        TblResponseEntity<PtFileEntity> tblResponseEntity = new TblResponseEntity<>();
        Integer total = fileDao.queryFileListTotal(entity);
        tblResponseEntity.setTotal(total);
        //计算当前页面数
        paging.deal(PtFileEntity.class,total);
        tblResponseEntity.setPage(paging.getPage());

        List<PtFileEntity> list = fileDao.queryFileInfoListByPage(entity,paging);
        tblResponseEntity.setRawRecords(list);

        tblResponseEntity.deal();

        return tblResponseEntity;
    }



    @Override
    public PtFileEntity queryFileInfoById(String fileUuid) {
        return fileDao.queryFileInfoById(fileUuid);
    }

    @Override
    public List<PtFileEntity> queryFileListAll() {
        return fileDao.queryFileListAll();
    }

    @Override
    public String generateTemporaryShareUrl(String obsKey, int expireSeconds) {
        // 从FileIoService中获取已初始化的ObsClient
        ObsClient obsClient = ((FileIoServiceImpl) fileIoService).getObsClient();

        // 创建临时签名请求（复用用户提供的核心代码）
        TemporarySignatureRequest request = new TemporarySignatureRequest();
        request.setBucketName(((FileIoServiceImpl) fileIoService).getBucketName()); // 从FileIoService获取存储桶名称
        request.setObjectKey(obsKey); // 文件在OBS中的标识
        request.setRequestDate(new Date()); // 当前时间
        request.setExpires(expireSeconds); // 有效期（秒）

        // 生成签名URL
        TemporarySignatureResponse signature = obsClient.createTemporarySignature(request);
        return signature.getSignedUrl();
    }



    @Override
    public List<ZtreeEntity> queryFileAcl(String fileUuid) {
        return null;
    }


    @Override
    public void setFileAcl(String fileUuid, String aclArray) {

    }

    @Override
    public List<ZtreeEntity> queryAclZtree() {
        return null;
    }

    @Override
    public PtFileEntity queryParentFileById(String fileUuid) {
        return fileDao.queryParentFileById(fileUuid);
    }

}
